package com.enation.app.javashop.api.client;

import com.enation.app.javashop.model.distribution.dos.DistributionOrderDO;
import com.enation.app.javashop.model.trade.order.dos.OrderDO;
import com.enation.app.javashop.service.distribution.DistributionOrderManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 分销Order client controller
 *
 * @author Chopper
 * @version v1.0
 * @since v6.1
 * 2016年10月2日 下午5:24:14
 */
@RestController
@RequestMapping("/client/distribution")
@Validated
@ApiIgnore
public class DistributionOrderClientController{

    @Autowired
    private DistributionOrderManager distributionOrderManager;
    /**
     * 根据sn获得分销商订单详情
     *
     * @param orderSn 订单编号
     * @return FxOrderDO
     */
    @RequestMapping(value = "/order/get", method = RequestMethod.POST)
    public DistributionOrderDO getModelByOrderSn(@RequestParam("order_sn") String orderSn){
        return distributionOrderManager.getModelByOrderSn(orderSn);
    }

    /**
     * 保存一条数据
     *
     * @param distributionOrderDO
     * @return
     */
    @RequestMapping(value = "/order/add", method = RequestMethod.POST)
    public DistributionOrderDO add(@RequestBody DistributionOrderDO distributionOrderDO){
        return distributionOrderManager.add(distributionOrderDO);
    }

    /**
     * 通过订单id，计算出各个级别的返利金额并保存到数据库
     *
     * @param orderSn 订单编号
     * @return 计算结果 true 成功， false 失败
     */
    @RequestMapping(value = "/order/cal", method = RequestMethod.POST)
    public boolean calCommission(@RequestParam("order_sn") String orderSn){
        return distributionOrderManager.calCommission(orderSn);
    }

    /**
     * 根据购买人增加上级人员订单数量
     *
     * @param buyMemberId 购买人会员id
     */
    @RequestMapping(value = "/num/add", method = RequestMethod.POST)
    public void addOrderNum(@RequestParam("buy_member_id") Long buyMemberId){
        this.distributionOrderManager.addOrderNum(buyMemberId);
    }


    /**
     * 计算退款时需要退的返利金额
     *
     * @param orderSn 订单sn
     * @param price   退款金额
     * @return 计算结果 true 成功， false 失败
     */
    @RequestMapping(value = "/order/num/cal/return", method = RequestMethod.POST)
    public boolean calReturnCommission(@RequestParam("order_sn") String orderSn, @RequestParam("price") double price){
        return this.distributionOrderManager.calReturnCommission(orderSn,price);
    }

    @PostMapping("/order/confirm")
    public void confirm(@RequestBody OrderDO order) {
        distributionOrderManager.confirm(order);
    }

}
